<?php
/**
 * Created by PhpStorm.
 * Author: Shadow
 * Date: 2022/9/13
 * Time: 2:24 下午
 * description: TypeBusiness.php
 */

namespace app\adminapi\business;


use app\common\model\Attribute;
use app\common\model\Spec;
use app\common\model\SpecValue;

use think\Container;
use think\Exception;
use think\facade\Db;

class TypeBusiness
{
    /**
     * 类型的修改
     * @param $id
     * @param $data
     */
    public static function update($id,$data) {
        // 启动事务
        Db::startTrans();
        try {
            // 将模型名修改
            \app\common\model\Type::update(['id' => $id,'type_name' => $data['type_name']]);
            //处理规格名
            $spec_data = [];
            foreach ($data['spec'] as $item) {
                $spec_data[] = [
                    'type_id' => $id,
                    'spec_name' => $item['name'],
                    'sort' => $item['sort']
                ];
            }

            Spec::where('type_id',$id)->delete(); //将旧的规格批量删除
            // 将新规格批量添加
            $spec = invoke(Spec::class)->saveAll($spec_data);// $spec = (new Spec())->saveAll($spec_data);
            $spec = $spec->toArray();
            $spec = array_column($spec,'id','spec_name');// 以规格名作为key、以规格名的id作为值

            // 将旧规格值批量删除
            SpecValue::where('type_id',$id)->delete();
            // 处理出添加规格值的结果集
            $spec_value_data = [];
            foreach ($data['spec'] as $item) {
                foreach ($item['value'] as $value) {
                    $spec_value_data[] = [
                        'type_id' => $id,
                        'spec_value' => $value,
                        'spec_id' => $spec[$item['name']],//规格名的id
                    ];
                }
            }
            //将规格值批量添加
            invoke(SpecValue::class)->saveAll($spec_value_data);

            Attribute::where('type_id',$id)->delete();//将属性的数据批量删除
            //处理出添加属性的结果集
            $attr_data = [];
            foreach ($data['attr'] as $value) {
                $attr_data[] =[
                    'type_id' => $id,
                    'attr_name' => $value['name'],
                    'attr_values' => implode(',',$value['value'])
                ];
            }
            //将属性放入属性表中
            invoke(Attribute::class)->saveAll($attr_data);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new Exception("删除失败");
        }

    }

    /**
     * 保存
     */
    public static function save($param){
        // 启动事务
        Db::startTrans();
        try {
            # 操作数据表CURD
            //        $param = [
//            'type_name' => "手机",
//            'spec' => [
//                [
//                    'name' => '颜色',
//                    'sort' => 1,
//                    'value' => ['白色','黑色']
//                ],
//                [
//                    'name' => '内存',
//                    'sort' => 1,
//                    'value' => ['64G','128G']
//                ],
//            ],
//        ];

            # 1、类型表
            $type = \app\common\model\Type::create([
                'type_name' => $param['type_name']
            ]);
            $type_id = $type->id;//新增的id

            # 2、规格表
            # 构造规格入库的数据
            $spec_data = [];
            foreach ($param['spec'] as $item) {
                $spec_data[] = [
                    'type_id' => $type_id,
                    'spec_name' => $item['name'],
                    'sort' => $item['sort']
                ];
            }
            # 批量添加
            $spec = (new Spec())->saveAll($spec_data);
            $spec = $spec->toArray();
            $spec = array_column($spec,'id','spec_name');// 以规格名作为key、以规格名的id作为值
//        ['颜色' => 1,'内存' => 2]
            # 3、规格值表
            # 构造入库数据
            $spec_value_data = [];
            foreach ($param['spec'] as $item) {
//            [
//                'name' => '颜色',
//                'value' => ['白色','黑色']
//            ]
                foreach ($item['value'] as $value) {
                    $spec_value_data[] = [
                        'type_id' => $type_id,
                        'spec_value' => $value,
                        'spec_id' => $spec[$item['name']],//规格名的id
                    ];
                }
            }
            (new SpecValue())->saveAll($spec_value_data);
            # 4、属性表
            # 构造数据
            $attr_data = [];
            foreach ($param['attr'] as $value) {
                $attr_data[] =[
                    'type_id' => $type_id,
                    'attr_name' => $value['name'],
                    'attr_values' => implode(',',$value['value'])
                ];
            }
            (new Attribute())->saveAll($attr_data);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new Exception("添加失败");
        }

    }

    /**
     * 删除模型
     */

    public static function delete($id) {
        // 启动事务
        Db::startTrans();
        try {
           # 删除类型
            \app\common\model\Type::destroy($id);
            # 删除规格名
            Spec::where('type_id',$id)->delete();
            # 删除规格值
            SpecValue::where('type_id',$id)->delete();
            # 删除属性信息
            Attribute::where('type_id',$id)->delete();
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new Exception("删除失败");
        }
    }
}